package com.itheima.controller.backend.controller;

import com.itheima.common.dto.CheckGroupDto;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.ICheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private ICheckGroupService checkGroupService;

    /**
     * 根据套餐id查询关联的检查组id
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(@RequestParam Integer setmealId) {
        return new Result(true, "查询检查组成功", checkGroupService.findCheckGroupIdsBySetmealId(setmealId));
    }

    // 根据id查询检查组
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        return new Result(true, "查询检查组成功", checkGroupService.selectCheckGroupById(id));
    }

    // 分页查询
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody(required = false) CheckGroupDto checkGroupDto) {
        return checkGroupService.selectCheckGroupList(checkGroupDto);
    }

    // 新增检查组
    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup) {
        checkGroupService.insertCheckGroup(checkitemIds, checkGroup);
        return new Result(true, "新增检查组成功", null);
    }

    // 修改检查组
    @PostMapping("/edit")
    public Result update(@RequestParam List<Integer> checkitemIds,@RequestBody CheckGroup checkGroup){
        checkGroupService.updateCheckGroup(checkitemIds,checkGroup);
        return new Result(true, "修改检查组成功", null);
    }

    // 删除检查组
    @GetMapping("/deleteById")
    public Result delete(@RequestParam Integer id) {
        checkGroupService.deleteCheckGroupById(id);
        return new Result(true, "删除检查组成功", null);
    }

    /**
     * 查询所有检查组
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = checkGroupService.selectCheckGroupAll();
        return new Result(true, "查询检查组成功", checkGroupList);
    }

}
